#!/bin/sh

# build the C test file ...

scriptname="$0"
if [ "${scriptname#/}" = "${scriptname}" ]; then
    scriptname="${PWD}/${scriptname}"
fi
scriptdir="$(dirname ${scriptname})"
srcdir="$(dirname $(dirname $(dirname $scriptdir)))"

rm -f "$2.c"
cat >>"$2.c" <<EOF
/* net-snmp standard headers */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/library/large_fd_set.h>
#include "snmplib/transports/snmpIPBaseDomain.h"
#include <utilities/execute.h>

/* testing specific header */
#include <net-snmp/library/testing.h>

/* standard headers */
#include <errno.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETINET_IP6_H
#include <netinet/ip6.h>
#endif
#ifndef HAVE_INET_PTON
#include "snmplib/inet_pton.h"
#endif
#ifndef HAVE_INET_NTOP
#include "snmplib/inet_ntop.h"
#endif

#define ABS_SRCDIR "${srcdir}"

int
main(int argc, char *argv[]) {

EOF
echo >>"$2.c" "#line 1 \"$1\""
cat >>"$2.c" "$1"
cat >>"$2.c" <<EOF

   if (__did_plan == 0) {
       PLAN(__test_counter);
   }

   return(0);
}

EOF

# ... and compile it.
${builddir}/libtool --mode=link `${builddir}/net-snmp-config --build-command` -I$builddir/include -I$srcdir -I$srcdir/include -I$srcdir/agent/mibgroup -o $2 $2.c ${builddir}/snmplib/libnetsnmp.la ${builddir}/agent/libnetsnmpmibs.la `${builddir}/net-snmp-config --external-libs`
echo $2
